home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / fileutil.zip / MKDIR.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  7KB  |  268 lines

  1. /* mkdir -- make directories
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  18.    This port is also distributed under the terms of the
  19.    GNU General Public License as published by the
  20.    Free Software Foundation.
  21.  
  22.    Please note that this file is not identical to the
  23.    original GNU release, you should have received this
  24.    code as patch to the official release.  */
  25.  
  26. #ifdef MSDOS
  27. static char RCS_Id[] =
  28.   "$Header: e:/gnu/fileutil/RCS/mkdir.c 1.4.0.2 90/09/19 12:10:16 tho Exp $";
  29.  
  30. static char Program_Id[] = "mkdir";
  31. static char RCS_Revision[] = "$Revision: 1.4.0.2 $";
  32.  
  33. #define VERSION \
  34.   "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  35.   (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
  36.  
  37. #define COPYING \
  38.   "This is free software, distributed under the terms of the\n" \
  39.   "GNU General Public License.  For details, see the file COPYING.\n"
  40. #endif /* MSDOS */
  41.  
  42. /* Usage: mkdir [-p] [-m mode] [+path] [+mode mode] dir...
  43.  
  44.    Options:
  45.    -p, +path        Ensure that the given path(s) exist:
  46.             Make any missing parent directories for each argument.
  47.             Parent dirs default to umask modified by `u+wx'.
  48.             Do not consider an argument directory that already
  49.             exists to be an error.
  50.    -m, +mode mode    Set the mode of created directories to `mode', which is
  51.             symbolic as in chmod and uses the umask as a point of
  52.             departure.
  53.  
  54.    David MacKenzie <djm@ai.mit.edu>  */
  55.  
  56. #include <stdio.h>
  57. #include <getopt.h>
  58. #include <sys/types.h>
  59. #include "system.h"
  60. #include "modechange.h"
  61.  
  62. #ifdef STDC_HEADERS
  63. #include <errno.h>
  64. #include <stdlib.h>
  65. #else
  66. extern int errno;
  67. #endif
  68.  
  69. #ifdef MSDOS
  70.  
  71. #include <direct.h>
  72. #include <io.h>
  73. #include <gnulib.h>
  74.  
  75. extern void main (int argc, char **argv);
  76. static int make_path (char *path, unsigned short mode,\
  77.               unsigned short parent_mode);
  78. static void strip_trailing_slashes (char *path);
  79. static void usage (void);
  80.  
  81. #define mkdir(path, mode)    mkdir (path)
  82.  
  83. #else /* not MSDOS */
  84.  
  85. int make_path ();
  86. void error ();
  87. void strip_trailing_slashes ();
  88. void usage ();
  89.  
  90. #endif /* not MSDOS */
  91.  
  92. /* If nonzero, ensure that a path exists.  */
  93. int path_mode;
  94.  
  95. /* The name this program was run with. */
  96. char *program_name;
  97.  
  98. struct option longopts[] =
  99. {
  100. #ifdef MSDOS
  101.   {"copying", 0, NULL, 30},
  102.   {"version", 0, NULL, 31},
  103. #endif
  104.   {"mode", 1, NULL, 'm'},
  105.   {"path", 0, &path_mode, 1},
  106.   {NULL, 0, NULL, 0}
  107. };
  108.  
  109. void
  110. main (argc, argv)
  111.      int argc;
  112.      char **argv;
  113. {
  114.   unsigned short newmode;
  115.   unsigned short parent_mode;
  116.   struct mode_change *change;
  117.   char *symbolic_mode;
  118.   int errors = 0;
  119.   int optc;
  120.   int ind;
  121.  
  122.   program_name = argv[0];
  123.   path_mode = 0;
  124.   symbolic_mode = NULL;
  125.  
  126.   while ((optc = getopt_long (argc, argv, "pm:", longopts, &ind)) != EOF)
  127.     {
  128.       switch (optc)
  129.     {
  130.     case 0:            /* Long option. */
  131.       break;
  132.     case 'p':
  133.       path_mode = 1;
  134.       break;
  135.     case 'm':
  136.       symbolic_mode = optarg;
  137.       break;
  138. #ifdef MSDOS
  139.     case 30:
  140.       fprintf (stderr, COPYING);
  141.       exit (0);
  142.       break;
  143.     case 31:
  144.       fprintf (stderr, VERSION);
  145.       exit (0);
  146.       break;
  147. #endif
  148.     default:
  149.       usage ();
  150.     }
  151.     }
  152.  
  153.   if (optind == argc)
  154.     usage ();
  155.   
  156.   newmode = 0777 & ~umask (0);
  157.   parent_mode = newmode | 0300;    /* u+wx */
  158.   if (symbolic_mode)
  159.     {
  160.       change = mode_compile (symbolic_mode, 0);
  161.       if (change == MODE_INVALID)
  162.     error (1, 0, "invalid mode");
  163.       else if (change == MODE_MEMORY_EXHAUSTED)
  164.     error (1, 0, "virtual memory exhausted");
  165.       newmode = mode_adjust (newmode, change);
  166.     }
  167.  
  168.   for (; optind < argc; ++optind)
  169.     {
  170.       strip_trailing_slashes (argv[optind]);
  171.       if (path_mode)
  172.     errors |= make_path (argv[optind], newmode, parent_mode);
  173.       else if (mkdir (argv[optind], newmode))
  174.     {
  175.       error (0, errno, "cannot make directory `%s'", argv[optind]);
  176.       errors = 1;
  177.     }
  178.     }
  179.  
  180.   exit (errors);
  181. }
  182.  
  183. /* Make sure directory `path' and all leading directories exist,
  184.    and give it permission mode `mode'.
  185.    If any leading directories are created, give them permission
  186.    mode `parent_mode'.
  187.    Return 0 if successful, 1 if errors occur. */
  188.  
  189. int
  190. make_path (path, mode, parent_mode)
  191.      char *path;
  192.      unsigned short mode;
  193.      unsigned short parent_mode;
  194. {
  195.   char *slash;
  196.   struct stat stats;
  197.  
  198.   if (stat (path, &stats))
  199.     {
  200.       slash = path;
  201.       while (*slash == '/')
  202.     slash++;
  203.       while (slash = index (slash, '/'))
  204.     {
  205.       *slash = 0;
  206.       if (stat (path, &stats))
  207.         {
  208.           if (mkdir (path, parent_mode))
  209.         {
  210.           error (0, errno, "cannot make directory `%s'", path);
  211.           return 1;
  212.         }
  213.         }
  214.       else if ((stats.st_mode & S_IFMT) != S_IFDIR)
  215.         {
  216.           error (0, 0, "`%s' is not a directory", path);
  217.           return 1;
  218.         }
  219.       *slash++ = '/';
  220.     }
  221.  
  222.       if (mkdir (path, mode))
  223.     {
  224.       error (0, errno, "cannot make directory `%s'", path);
  225.       return 1;
  226.     }
  227.     }
  228.   else if ((stats.st_mode & S_IFMT) != S_IFDIR)
  229.     {
  230.       error (0, 0, "`%s' is not a directory", path);
  231.       return 1;
  232.     }
  233.   else if (chmod (path, mode))
  234.     {
  235.       error (0, errno, "cannot change mode of `%s'", path);
  236.       return 1;
  237.     }
  238.   return 0;
  239. }
  240.  
  241. /* Remove trailing slashes from PATH; they cause some system calls to fail. */
  242.  
  243. void
  244. strip_trailing_slashes (path)
  245.      char *path;
  246. {
  247.   int last;
  248.  
  249.   last = strlen (path) - 1;
  250.   while (last > 0 && path[last] == '/')
  251.     path[last--] = '\0';
  252. }
  253.  
  254. void
  255. usage ()
  256. {
  257. #ifdef MSDOS
  258.   fprintf (stderr, "\
  259. Usage: %s [-p] [-m mode] [+path] [+mode mode] [+copying] \n\
  260.        [+version] dir...\n", program_name);
  261. #else
  262.   fprintf (stderr, "\
  263. Usage: %s [-p] [-m mode] [+path] [+mode mode] dir...\n",
  264.        program_name);
  265. #endif
  266.   exit (1);
  267. }
  268.